
import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/ware_house_entity.dart';

import '../../bean/lot_entity.dart';
import '../../bean/product_lib_entity.dart';
import '../../http/api/lot_api.dart';
import '../../routes/Routes.dart';
import '../../views/search_drop_down_select/search_drop_down_select_widget.dart';

class HalfProductIntoLibController extends BaseController{

  String selWarehouse = "";
  String selStorage = "";

  List<Map<dynamic, dynamic>> mWarehouseList = [];
  Map<dynamic, dynamic>? selectWarehouseObj;

  List<Map<dynamic, dynamic>> mStorageList = [];
  Map<dynamic, dynamic>? selectStorageObj;

  List<LotEntity> lotList = [];

  TextEditingController textEditingController = TextEditingController();

  String LOTID = 'LOTID';
  String WAREHOUSEID = 'WAREHOUSEID';
  String STORAGEID = 'STORAGEID';

  List<WareHouseWAREHOUSES> allList = [];
  List<String> lotData = [];


  @override
  void onInit() {
    super.onInit();

  }

  @override
  void onReady(){
    super.onReady();
    getWarehouseList();
  }

  lotInfo(String lot) async{
    Map<String, dynamic> params = {'LOTID':lot};
    var result = await LotApi.lotInfo(canelToken,params: params);
    if(result.ok){
      LotEntity lotEntity = result.data ?? LotEntity();
      if(lotData.contains(lotEntity.lOTID)){
        Get.snackbar('提示', '当前批次已添加');
      }else{
        lotData.add(lotEntity.lOTID ?? '');
        lotList.add(lotEntity);
        update([LOTID]);
      }
      // lotList.forEach((element) {
      //   if(element.lOTID == lotEntity.lOTID){
      //
      //     return;
      //   }
      // });
      textEditingController.text = '';
    }
  }

  lotShip() async{
    if(lotList.isEmpty){
      Get.snackbar('提示', '请先添加批次列表');
      return;
    }

    if(selWarehouse.isEmpty){
      Get.snackbar('提示', '请选择仓库');
      return;
    }

    if(selStorage.isEmpty){
      Get.snackbar('提示', '请选择位置');
      return;
    }

    List<ProductLibEntity> data = [];
    lotList.forEach((element) {
      ProductLibEntity entity = ProductLibEntity();
      entity.LOTID = element.lOTID;
      entity.ATTRIBUTE4 = element.CARRIER_ID;
      entity.LOTCOMMENT = element.CYCLE;
      data.add(entity);
    });

    Map<String, dynamic> params = {'ACTIONTYPE':'S','LOTLIST' : data,'TRANSWAREHOUSEID' : selWarehouse,'TRANSSTORAGEID' : selStorage};
    var result = await LotApi.getLotShip(canelToken,params: params);
    if(result.ok){
      Fluttertoast.showToast(
        msg: "入库成功",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
      );

      Get.offAllNamed(Routes.Tabs);
    }


  }


  void updateSelectWarehouseValue(String str) {
    selWarehouse = str;
    mStorageList.clear();
    selectStorageObj = ({SearchDropDownSelectWidget.label: '',  SearchDropDownSelectWidget.value: ''});
    selStorage = '';
    allList.forEach((element) {
        if(element.wAREHOUSEID == str){
          element.sTORAGES?.forEach((element) {
            mStorageList.add({
              SearchDropDownSelectWidget.label: element.dESCRIPTION ?? "",
              SearchDropDownSelectWidget.value: element.nAME
            });
          });
        }
    });

    update([STORAGEID]);
  }

  void updateSelectStorageValue(String str) {
    selStorage = str;
  }


  void getWarehouseList() async {
    Map<String, dynamic> params = {'ACTIONTYPE':'Q'};
    var result = await LotApi.getWarehouseList(canelToken,params: params);
    if(result.ok){
      var data = result.data;
      allList.addAll(data?.wAREHOUSES ?? []);
      allList.forEach((element) {
        mWarehouseList.add({
          SearchDropDownSelectWidget.label: element.dESCRIPTION ?? "",
          SearchDropDownSelectWidget.value: element.wAREHOUSEID
        });
      });
    }

    update([WAREHOUSEID]);
  }
}